#include <iostream>
#include "../include/calculator.h"

// Function to display a welcome message
void displayWelcome() {
    std::cout << "Welcome to the Calculator App!" << std::endl;
    std::cout << "=============================" << std::endl;
}

// Function to display the menu
void displayMenu() {
    std::cout << "\nAvailable Operations:" << std::endl;
    std::cout << "1. Addition" << std::endl;
    std::cout << "2. Subtraction" << std::endl;
    std::cout << "3. Multiplication" << std::endl;
    std::cout << "4. Division" << std::endl;
    std::cout << "5. Power" << std::endl;
    std::cout << "6. Square Root" << std::endl;
    std::cout << "0. Exit" << std::endl;
    std::cout << "Enter your choice: ";
}

// Function to get user input for a single number
double getNumber() {
    double num;
    std::cout << "Enter a number: ";
    std::cin >> num;
    return num;
}

// Function to get user input for two numbers
void getTwoNumbers(double& a, double& b) {
    std::cout << "Enter first number: ";
    std::cin >> a;
    std::cout << "Enter second number: ";
    std::cin >> b;
}

// Function to process the user's choice
void processChoice(int choice, math::Calculator& calc) {
    double a, b, result;
    
    switch (choice) {
        case 0:
            std::cout << "Exiting program. Goodbye!" << std::endl;
            break;
        case 1:
            getTwoNumbers(a, b);
            result = calc.add(a, b);
            std::cout << a << " + " << b << " = " << result << std::endl;
            break;
        case 2:
            getTwoNumbers(a, b);
            result = calc.subtract(a, b);
            std::cout << a << " - " << b << " = " << result << std::endl;
            break;
        case 3:
            getTwoNumbers(a, b);
            result = calc.multiply(a, b);
            std::cout << a << " * " << b << " = " << result << std::endl;
            break;
        case 4:
            getTwoNumbers(a, b);
            try {
                result = calc.divide(a, b);
                std::cout << a << " / " << b << " = " << result << std::endl;
            } catch (const std::exception& e) {
                std::cerr << "Error: " << e.what() << std::endl;
            }
            break;
        case 5:
            std::cout << "Enter base: ";
            std::cin >> a;
            int exponent;
            std::cout << "Enter exponent (integer): ";
            std::cin >> exponent;
            result = calc.power(a, exponent);
            std::cout << a << " ^ " << exponent << " = " << result << std::endl;
            break;
        case 6:
            a = getNumber();
            try {
                result = calc.squareRoot(a);
                std::cout << "Square root of " << a << " = " << result << std::endl;
            } catch (const std::exception& e) {
                std::cerr << "Error: " << e.what() << std::endl;
            }
            break;
        default:
            std::cout << "Invalid choice. Please try again." << std::endl;
    }
}

int main() {
    displayWelcome();
    
    math::Calculator calculator;
    int choice;
    
    do {
        displayMenu();
        std::cin >> choice;
        
        processChoice(choice, calculator);
        
    } while (choice != 0);
    
    return 0;
} 